package com.smartstate.dataManagement.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.entity.*;
import com.smartstate.dataManagement.service.PermissionService;
import com.smartstate.dataManagement.util.SpringUtils;
import com.smartstate.dataManagement.util.StringUtils;
import com.smartstate.dataManagement.vo.ResultVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/Permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

//    @PostMapping("/saveOrUpdate")
//    @ApiOperation(value = "新增或修改")
//    public boolean saveOrUpdate(@RequestBody Permission permission) {
//        return permissionService.saveOrUpdate(permission);
//    }

//    @PostMapping("/removeById")
//    @ApiOperation(value = "根据ID删除（更新状态而已,即禁用和启用开关）")
//    public boolean removeById(@RequestParam String id) {
//        return permissionService.removeById(id);
//    }

    @PostMapping("/delByID")
    @ApiOperation(value = "根据ID删除数据（删除数据）")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id",paramType = "query",required = true),})
    public ResultVO<Boolean> delByID(@RequestParam String id) {
        if(StringUtils.isNotBlank(id)){
            return ResultVO.ok(permissionService.delByID(id));
        }
        return ResultVO.fail("数据缺失， 请输入参数");
    }

    @PostMapping("/delBatchByIDs")
    @ApiOperation(value = "根据ID集合批量删除数据（删除数据）")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "ids",paramType = "query",required = true),})
    public ResultVO<Boolean> delBatchByIDs(@RequestBody List<String> ids) {
        if(ids.size()>0){
            return ResultVO.ok(permissionService.delBatchByIDs(ids));
        }
        return ResultVO.fail("数据缺失， 请输入参数");
    }

    @PostMapping("/updateStatuByID")
    @ApiOperation(value = "根据ID更新启用和禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id",paramType = "query",required = true),
            @ApiImplicitParam(name = "status", value = "状态(0是禁用，1是启用）",paramType = "query",required = true),
    })
    public ResultVO<Boolean> updateStatuByID(@RequestParam String id,@RequestParam String  status) {
        if(StringUtils.isNotBlank(id) && StringUtils.isNotBlank(status)){
            return ResultVO.ok(permissionService.updateStatuByID(id,status));
        }
        return ResultVO.fail("数据缺失， 请输入参数");
    }


    @GetMapping("/findAll")
    @ApiOperation(value = "获取权限列表(全部都查询， 即state（0,1） ")
    public List<Permission> findAll() {
        return permissionService.findAll();
    }



    @PostMapping("/lazyLoadMeun")
    @ApiOperation(value = "根据父级ID懒加载权限信息(id为0表示顶级菜单")
    public ResultVO<List<Permission>> lazyLoadMeun(@RequestParam(required = true) String id) {
        return ResultVO.ok(permissionService.lazyLoadMeun(id));
    }


    @PostMapping("/loadAllMeun")
    @ApiOperation(value = "加载全部菜单")
    public ResultVO<List<Permission>> loadAllMenu() {
        return permissionService.loadAllMeun();
    }



    @PostMapping("/getPerByRoleID")
    @ApiOperation(value = "根据角色ID获取权限列表")
    public ResultVO<List<Permission>> getPerByRoleID(@RequestParam String roleId) {
        if(StringUtils.isNotBlank(roleId)){
            return permissionService.getPerByRoleID(roleId);
        }
        return ResultVO.fail("参数缺失，请输入角色ID");
    }



    @PostMapping ("/getPermByNameAndStatus")
    @ApiOperation(value = "单表分页查询权限，依据账户和状态分页查询")
    public Page<Permission> getPermByNameAndStatus(@RequestParam(required = false) String name, @RequestParam(required = false) String status,@RequestBody PageDTO<Permission> pageDTO) {
        return  permissionService.getPermByNameAndStatus(name,status,pageDTO);
    }




    @PostMapping ("/addChildOrFather")
    @ApiOperation(value = "新增数据(下级菜单)")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "parentId", value = "父级ID（0 表示主平台，其他表示下级菜单所属的父级ID ）",paramType = "query" ,required=true),
//            @ApiImplicitParam(name = "name", value = "菜单名称",paramType = "query" ,required=true),
//            @ApiImplicitParam(name = "type", value = "菜单类型 （0菜单 1按钮, 2读接口,3写接口）",paramType = "query",required = true),
//            @ApiImplicitParam(name = "urlPath", value = "路由信息",paramType = "query"),
//            @ApiImplicitParam(name = "sort", value = "菜单权重",paramType = "query"),
//            @ApiImplicitParam(name = "component", value = "菜单组件",paramType = "query"),
//    })
    public ResultVO<Permission> addChildOrFather(@RequestBody Permission  permission) {
        return  permissionService.addChildOrFather(permission);

    }



    @PostMapping ("/updateByPermID")
    @ApiOperation(value = "根据权限ID修改权限的基本信息")
    public ResultVO<Boolean> updateByPermID(@RequestBody Permission  permission) {
        if(null != permission){
            if(StringUtils.isNotBlank(permission.getId())){
                return  ResultVO.ok(permissionService.updateByPermID(permission));
            }
            return  ResultVO.fail("缺失必要参数， 请输入");
        }
        return  ResultVO.fail("参数为空， 请输入参数");
    }


}
